package com.cndw.kungfu.dao;


import java.util.List;
import java.util.Map;

import com.cndw.dao.jade.Identity;
import com.cndw.dao.jade.annotation.DAO;
import com.cndw.dao.jade.annotation.KeyColumnOfMap;
import com.cndw.dao.jade.annotation.SQL;
import com.cndw.dao.jade.annotation.SQLParam;
import com.cndw.kungfu.domain.Task;


@DAO
public interface TaskDao {

	public static final String TABLE = " kf_player_task ";
	
	public static final String FIELDS = " id,playerId,taskId,status,addTime,finishTime,quality";
	
	
	/**
	 * 添加一个任务
	 * @param task
	 * @return
	 */
	@SQL("insert into "+TABLE+" ("+FIELDS+") VALUES (0,:p.playerId,:p.taskId,:p.status,:p.addTime,:p.finishTime,:p.quality) ")
	public Identity insert(@SQLParam("p") Task task);	
	
	@SQL("select "+FIELDS+" from "+TABLE+" where playerId=:playerId and taskId=:taskId order by id asc limit 1 ")
	public Task getTask(@SQLParam("playerId") long playerId,@SQLParam("taskId") int taskId);
	
	
	/**
	 * 更新某个任务状态
	 * @param task
	 * @return
	 */
	@SQL("update  " + TABLE+ " set status=:p.status,addTime=:p.addTime,finishTime=:p.finishTime,quality=:p.quality  where playerId=:p.playerId and id=:p.id  ")
	public int updateStatus(@SQLParam("p") Task task);
	/**
	 * 获取我正在做的任务
	 * @param playerId
	 * @param type
	 * @return
	 */
	@SQL("select "+FIELDS + " from " + TABLE+ " where playerId=:playerId and status<:status order by taskId")
	public List<Task> getDoingTasks(
		@SQLParam("playerId")long playerId,
		@SQLParam("status")int status);	
	
	/**
	 * 提取我已经完成的任务
	 * @param playerId
	 * @param status
	 * @return
	 */
	@SQL("select taskId,finishTime from " + TABLE+ " where playerId=:playerId and status=:status order by id,taskId ")
	@KeyColumnOfMap("taskId")
	public Map<Integer, Integer> getENDTaskIds(
		@SQLParam("playerId")long playerId,
		@SQLParam("status") int status);
	
	/**
	 * 删除某个任务
	 * @param playerId
	 * @param taskId
	 */
	@SQL("delete from "+TABLE+" where playerId=:playerId and taskId=:taskId ")
	public void delete(@SQLParam("playerId")long playerId,@SQLParam("taskId") int taskId);

	
}
